﻿#include "game_state.h"

CGameState::CGameState(LPWSTR BackgroundPath)
{
	_IsEnabled = false;
	Background = NULL;
	_BackgroundPath = BackgroundPath;
}

CGameState::~CGameState()
{
	if(Background)
		Background->Release();
}

void CGameState::Show()
{
	_IsEnabled = false;
}

void CGameState::Hide()
{
	_IsEnabled = false;
}

void CGameState::InitElements(LPDIRECT3DDEVICE9 d3dDevice)
{
	Background = CreateSurfaceFromFile(d3dDevice, _BackgroundPath); //Load background
	D3DXCreateSprite(d3dDevice, &_SpriteHandler); //Khởi tạo sprite handler
}

void CGameState::UpdateElements(int t)
{

}

void CGameState::DrawElements()
{
	
}

void CGameState::ProcessInput(LPDIRECT3DDEVICE9 d3dDevice, int Delta, BYTE *KeyStates)
{
	
}

void CGameState::OnKeyDown(int KeyCode) 
{

}

void CGameState::OnKeyUp(int KeyCode)
{
	
}

int CGameState::_IsKeyDown(BYTE *KeyStates, int KeyCode)
{
	return (KeyStates[KeyCode] & 0x80) > 0;
}

LPDIRECT3DSURFACE9 CGameState::CreateSurfaceFromFile(LPDIRECT3DDEVICE9 d3dDevice, LPWSTR FilePath)
{
	D3DXIMAGE_INFO imgInfo;
	HRESULT hrs;

	//Load ảnh
	hrs = D3DXGetImageInfoFromFile(FilePath, &imgInfo);
	if(hrs != S_OK)
	{
		return NULL;
	}

	LPDIRECT3DSURFACE9 surface;

	d3dDevice->CreateOffscreenPlainSurface(
		imgInfo.Width,
		imgInfo.Height,
		D3DFMT_X8R8G8B8,
		D3DPOOL_DEFAULT,
		&surface,
		NULL);

	hrs = D3DXLoadSurfaceFromFile(
		surface, 
		NULL,
		NULL,
		FilePath,
		NULL,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL);

	if(hrs != S_OK)
	{
		return NULL;
	}

	return surface;
}
